package com.jidian.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jidian.entity.Course;
import com.jidian.entity.User;
import com.jidian.result.Result;
import com.jidian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 冯永龙、王艳玲、李斐
 * @since 2020-11-28
 */
@RestController
@RequestMapping("//user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/addUser")
    public boolean addUser(@RequestBody User user){
        return userService.save(user);
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        User result = this.userService.login(user);
        Result result1 = new Result(20000,"登录成功");
        Result result2 = new Result(20000,"用户名或者密码错误");
        if (result != null){    //返回true为用户存在可以登录，false为用户不存在拒绝登录
           return result1;
        }else {
            return result2;
        }
    }

    @PostMapping ("/teacher")
    public List<User> findTeacher(){
        return userService.getTeacher();
    }

    @PostMapping("/student")
    public List<User> findStudent(){
        return userService.getStudent();
    }

    @GetMapping("/deleteUser")
    public boolean deleteUser(Integer id){
        System.out.println(id);
       return userService.removeById(id);
    }

    @PostMapping("/updateUser")
    public boolean updateUser(User user){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name",user.getId());
        return userService.update(user,wrapper);
    }

//    @PostMapping("/selectByName")
//    public void selectByMySelect() {
//        List<User> users = userService.selectByName("王天风");
//        users.forEach(System.out::println);
//    }

}

